<template>
  <div class="app-container">
    <div v-if="!detailVisible">
      <div v-if="item.status != 1">
        <div>
          <div style="display: flex;justify-content: space-between;">
            <div style="font-weight: 600">{{ item.name }}</div>
            <div style="cursor: pointer;" class="reminder" @click="taskRemindMore">一键催单</div>
          </div>
          <div style="height: 100px">
            <ul class="detail-item">
              <li>
                <div class="label">当前状态</div>
                <div class="value">{{ getStatus(item.status) }}</div>
              </li>
              <li>
                <div class="label">填报周期</div>
                <!-- <div v-if="scope.row.frequency == 'monthly'">{{ scope.row.year + '年' + scope.row.key + '月' }}</div>
            <div v-else>{{ scope.row.year + '年' + scope.row.key }}</div> -->
                <div v-if="item.frequency == 'monthly'" class="value">{{ item.year+'年' + item.key + '月' }}</div>
                <div v-else class="value">{{ item.year + '年' + item.key }}</div>
              </li>
              <li>
                <div class="label">填报单位</div>
                <div class="value">{{ item.districts.length && item.districts[0].name }}</div>
              </li>
              <li>
                <div class="label">开始时间</div>
                <div class="value">{{ item.start_date }}</div>
              </li>
              <li>
                <div class="label">结束时间</div>
                <div class="value">{{ item.end_date }}</div>
              </li>
              <li>
                <div class="label">工单数</div>
                <div class="value">{{ detailItem.sub_tasks && detailItem.sub_tasks.length }}</div>
              </li>
              <li style="width: 200px">
                <div class="label">任务进度</div>
                <el-progress style="margin-top: 10px;" :percentage="parseFloat((item.progressing*100).toFixed(0))" />
              </li>
            </ul>
          </div>
          <el-divider />
        </div>
        <el-table
          ref="multipleTable"
          :data="detailItem.sub_tasks"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            :selectable="selectable"
            type="selection"
            width="55"
          />
          <el-table-column
            label="工单编号"
            prop="id"
            width="120"
          />
          <el-table-column
            prop="district_id"
            label="填报单位"
            width="140"
          >
            <template slot-scope="scope">
              <div>
                {{ scope.row.district && scope.row.district.name }}
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="status" width="140" label="工单状态">
            <template slot-scope="scope">
              <div>
                {{ getSubStatus(scope.row.status) }}
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="report_log" label="填报人">
            <template slot-scope="scope">
              <div>
                {{ scope.row.report_log && scope.row.report_log.operator && scope.row.report_log.operator.name }}
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="report_log" label="填报时间" show-overflow-tooltip>
            <template slot-scope="scope">
              <div>
                {{ scope.row.report_log && getTimeFormate(scope.row.report_log.created_at) }}
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="210" align="center">
            <template slot-scope="scope">
              <el-button type="text" @click="handleDetail(scope.row)">详情</el-button>
              <!-- <el-tag>
                    <i class="el-icon-arrow-right" />
                    标签一</el-tag> -->
              <!-- <el-tag>催单</el-tag> -->
              <el-button v-if="scope.row.status != 6" type="text" :disabled="btnDisabled(scope.row)" @click="taskRemind(scope.row)">催单</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div v-if="item.status == 1">
        <div>
          <div style="font-size: 18px;font-weight: 600">{{ detailItem.name }}</div>
          <el-divider />
        </div>
        <div class="detail1">
          <div style="">

            <div class="detail1-item">
              <p class="label">当前状态</p>
              <p class="value">{{ getStatus(detailItem.status) }}</p>
            </div>
            <div class="detail1-item">
              <p class="label">填报周期</p>
              <p class="value">{{ detailItem.year }}</p>
            </div>
            <div class="detail1-item">
              <p class="label">填报单位</p>
              <!-- <p class="value">{{ item.districts[0].name }}</p> -->
            </div>
            <div class="detail1-item">
              <p class="label">开始时间</p>
              <p class="value">{{ detailItem.start_date }}</p>
            </div>
            <div class="detail1-item">
              <p class="label">结束时间</p>
              <p class="value">{{ detailItem.end_date }}</p>
            </div>
          </div>
        </div>
      </div>
      <div style="text-align: center;margin-top: 80px;">
        <!-- <el-button v-if="item.status == 1" type="primary" style="width: 120px;" @click="subPublish">发布</el-button> -->
        <el-button type="primary" style="width: 120px" @click="goBack">返回</el-button>
      </div>
    </div>
    <div v-if="detailVisible">
      <Detail v-if="detailVisible" :item="taskItem" :detail-type="'detail'" @goBack="goBack2" />
    </div>
  </div>
</template>
<script>
import moment from 'moment'
import { taskPublish, getTaskDetail, taskSubRemind } from '@/api/table'
import Detail from './taskDetail.vue'
// import { getTimeFormater } from '@/utils/index'
export default {
  components: {
    Detail
  },
  props: {
    item: {
      type: Object,
      default: () => {}
    },
    districtList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      multipleSelection: [],
      detailItem: {},
      taskItem: {},
      detailVisible: false,
      subStatus: [
        {
          value: '1',
          label: '待处理'
        }, {
          value: '2',
          label: '待初审'
        },
        {
          value: '3',
          label: '初审驳回'
        },
        {
          value: '4',
          label: '待复审'
        },
        {
          value: '5',
          label: '复审驳回'
        },
        {
          value: '6',
          label: '已完结'
        }
      ],
      // btnDisabled: false,
      publishStatus: [
        {
          value: '1',
          label: '未开始'
        },
        {
          value: '2',
          label: '进行中'
        },
        {
          value: '3',
          label: '已完成'
        },
        {
          value: '4',
          label: '已延期'
        },
        {
          value: '5',
          label: '已撤回'
        }]
    }
  },
  created() {
    this.getDetail()
  },
  methods: {
    handleSelectionChange(val) {
      val.map(item => {
        this.multipleSelection.push(item.id)
      })
    },
    getDetail() {
      getTaskDetail({ id: this.item.id }).then(res => {
        this.detailItem = res.data
      })
    },
    getTimeFormate(date) {
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    },
    subPublish() {
      taskPublish(this.item).then(res => {
        // console.log(res.data, '------------------res')
      })
    },
    goBack2() {
      this.detailVisible = false
    },
    selectable(row, index) {
      if (row.status != 6) {
        return true
      } else {
        return false
      }
    },
    handleDetail(row) {
      this.detailVisible = true
      this.taskItem = row
      // const routeUrl = this.$router.resolve({
      //   path: '/Task/taskDetail',
      //   query: {
      //     id: row.id
      //     // type: 'detail'
      //   }
      // })
      // window.open(routeUrl.href, '_blank')
      // const url = `${window.location.origin}/workOrder?id=${row.id}`
      // window.open(url)
    },
    btnDisabled(row) {
      const todayDate = new Date().setHours(0, 0, 0, 0) // 把今天的日期时分秒设置为00：00：00，返回一个时间戳,
      const paramsDate = new Date(row.last_reminded_at).setHours(0, 0, 0, 0)// 给new Date()传入时间，并返回传入时间的时间戳
      // let isToday = (todayDate===paramsDate);//时间戳相同时 True 就为今天
      console.log(row.last_reminded_at, todayDate, paramsDate, '-------------------------btnDisabled')
      return todayDate === paramsDate
    },
    taskRemindMore() {
      if (!this.multipleSelection.length) {
        this.$message({
          message: '请先选择工单',
          type: 'warning'
        })
      } else {
        taskSubRemind({ sub_task_ids: this.multipleSelection }).then(res => {
          if (res.status === 200) {
            // this.btnDisabled = true
            this.$message({
              message: '催单成功',
              type: 'success'
            })
          }
          this.getDetail()
          console.log(res, '--------res')
        })
      }
    },
    taskRemind(row) {
      taskSubRemind({ sub_task_ids: [row.id] }).then(res => {
        if (res.status === 200) {
          // this.btnDisabled = true
          this.$message({
            message: '催单成功',
            type: 'success'
          })
        }
        this.getDetail()
        console.log(res, '--------res')
      })
    },
    getStatus(status) {
      let label = ''
      this.publishStatus.forEach(item => {
        if (item.value == status) {
          label = item.label
        }
      })
      return label
    },
    getSubStatus(status) {
      let label = ''
      this.subStatus.forEach(item => {
        if (item.value == status) {
          label = item.label
        }
      })
      return label
    },
    goBack() {
      this.$emit('goBack')
    }
  }
}
</script>
<style lang="scss" scoped>
.detail1 {
  width: 100%;
  // height: 100vh;
  display: flex;
  justify-content: center;
  // align-items: center;
  margin-top: 80px;
  .detail1-item {
    margin: 60px 0;
    p {
      text-align: center;
      margin-bottom: 15px;
    }
    .label {
      color: #acacac;
      font-size: 14px;
    }
    .value {
      font-size: 14px;
    }
  }
}
.detail-btn {
  text-align: center;
}
.detail-item {
  height: 100%;
  display: flex;
  justify-content: space-around;
  align-items: center;
  .label {
    font-size: 14px;
    color: #aaaaaa;
  }
  .value {
    margin-top: 10px;
    font-size: 14px;
    // text-align: center;
    // font-size: 30px;
  }
}
</style>
